package com.example.weiting.huishenghuo.com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.weiting.huishenghuo.R;
import com.example.weiting.huishenghuo.utils.Message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ModifyDelete extends AppCompatActivity {
    public final static int RESULT_CODE = 2;
    List<Message> messageList;
    EditText et1,et;
    int id;
    RadioButton rb,rb1;
    Intent intent;
    Bundle args,bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_delete);

        //获取其他页面传来的messageList和id。
        intent = getIntent();
        args = intent.getBundleExtra("messages");
        messageList = (ArrayList<Message>) args.getSerializable("messageList");
        bundle = intent.getBundleExtra("id");
        id = (int)bundle.getSerializable("id");

        //获取页面的组件并赋值
        rb = (RadioButton) findViewById(R.id.MD);
        rb1 = (RadioButton) findViewById(R.id.MD1);
        if(messageList.get(id).getZhuangTai() > 0){
            rb1.setChecked(true);
        }else{
            rb.setChecked(true);
        }
        et = (EditText) findViewById(R.id.MDeditText);
        et.setText(messageList.get(id).getThing());
        et1 = (EditText) findViewById(R.id.MDeditText1);
        et1.setText(Math.abs(messageList.get(id).getZhuangTai())+"");
        TextView tv = (TextView) findViewById(R.id.MDtextView);
        tv.setText(messageList.get(id).getDate().toString());

        //保存按钮功能的实现
        Button bt = (Button) findViewById(R.id.MDbutton);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //修改数据
                messageList.get(id).setThing(et.getText().toString());
                if (rb.isChecked()) {
                    messageList.get(id).setZhuangTai(Integer.parseInt("-"+et1.getText().toString()));
                }else{
                    messageList.get(id).setZhuangTai(Integer.parseInt(et1.getText().toString()));

                }
                //返回结果集合messageList
                args.putSerializable("messageList",(Serializable)messageList);
                intent.putExtra("messages",args);
                setResult(RESULT_CODE, intent);
                finish();
            }
        });

        //删除功能按钮的实现
        Button bt1 = (Button) findViewById(R.id.MDbutton1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                messageList.remove(id);
                //返回结果集合messageList
                args.putSerializable("messageList",(Serializable)messageList);
                intent.putExtra("messages",args);
                setResult(RESULT_CODE, intent);
                finish();
            }
        });
    }
}
